import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Admin from '../views/Admin.vue'

//页面路由组件
import Index from '../components/admin/Index'
import AddArt from '../components/article/AddArt'
import ArtList from '../components/article/ArtList'
import CateList from '../components/category/CateList'
import UserList from '../components/user/UserList'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    children: [
      {path:'index',component:Index},
      {path:'addart',component:AddArt},
      {path:'artlist',component:ArtList},
      {path:'catelist',component:CateList},
      {path:'userlist',component:UserList},
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//类似于拦截器的切面变成
router.beforeEach((to,from,next)=>{
  const token = window.sessionStorage.getItem('token')
  if (to.path === '/login') return next()
  if (!token && to.path === '/admin'){
    next('/login')
  }else {
    next()
  }
})

export default router
